﻿Imports System.Text.RegularExpressions

Public Class frmSolicitante
    Dim objBLL As New BLLSolicitante
    Public tipo As String
    Public Codigo As Integer

#Region "Atualização"
    Sub limpa()
        txtsolicitante.Text = ""
        txtempresa.Text = ""
        mktelefone.Text = ""
        mkcelular.Text = ""
        txtemail.Text = ""
        txtobservacao.Text = ""
        txtcidade.Text = ""
        txtsolicitante.Select() 'manter o focu no primeiro textbox
    End Sub
    Sub CarregaPropriedades()
        objBLL.pCodigo = Codigo
        objBLL.pnome = Trim(txtsolicitante.Text)
        objBLL.pempresa = Trim(txtempresa.Text)
        If Trim(mktelefone.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "") <> "" Then
            objBLL.ptelefone = Trim(mktelefone.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "")
        End If
        If Trim(mkcelular.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "") <> "" Then
            objBLL.pcelular = Trim(mktelefone.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "")
        End If
        objBLL.pcidade = txtcidade.Text
        objBLL.pemail = Trim(txtemail.Text)
        objBLL.pobs = Trim(txtobservacao.Text)
    End Sub
    Sub CarregaCampos(ByVal codigo As Integer)
        Dim tab As DataTable
        tab = objBLL.localizar(codigo)
        lbCodigo.Visible = True
        lbTitulo.Text = tab.Rows(0)("ext_nome").ToString()
        lbCodigo.Text = "Código: " & codigo
        txtsolicitante.Text = tab.Rows(0)("ext_nome").ToString()
        txtempresa.Text = tab.Rows(0)("ext_empresa").ToString()
        txtcidade.Text = tab.Rows(0)("ext_cidade").ToString()
        mktelefone.Text = tab.Rows(0)("ext_telefone").ToString()
        mkcelular.Text = tab.Rows(0)("ext_celular").ToString()
        txtemail.Text = tab.Rows(0)("ext_email").ToString()
        txtobservacao.Text = tab.Rows(0)("ext_obs").ToString()
    End Sub

    Sub Atualiza()
        fMenu.TPRelacionamento.Controls.Clear()
        Dim uc = New ucCadastroSolicitante()
        uc.Dock = DockStyle.Fill
        fMenu.TPRelacionamento.Controls.Add(uc)
        uc = Nothing
    End Sub
#End Region
#Region " Validação"
    Private Sub txtsolicitante_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtsolicitante.KeyPress
        If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = " " And Not e.KeyChar = "." Then
            e.Handled = True
        End If
    End Sub
    Private Sub txtcidade_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtcidade.KeyPress
        If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = " " Then
            e.Handled = True
        End If
    End Sub
    Function ValidaEmail(ByVal email As String) As Boolean
        If email = "" Then
            Return True
        Else
            Dim strRegex As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + "\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + ".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
            Dim re As New Regex(strRegex)
            If (re.IsMatch(email)) Then
                Return True
            Else
                Return False
            End If
        End If
    End Function
#End Region
    Private Sub frmCadastroSolicitante_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        limpa()
        Select Case tipo
            Case "Novo"
                tsbAdicionar.Visible = True
                tsbAlterar.Visible = False
                lbTitulo.Text = "Solicitante Externo"
                lbCodigo.Visible = False
            Case "Alterar"
                tsbAlterar.Visible = True
                tsbAdicionar.Visible = False
                CarregaCampos(Codigo)
            Case Else

        End Select
    End Sub
    Private Sub tsbVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbVoltar.Click
        Me.Close()
    End Sub
    Private Sub tsbAdicionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbAdicionar.Click
        Try
            Dim ret As Boolean
            If Trim(txtsolicitante.Text) <> "" And Trim(txtempresa.Text) <> "" And Trim(txtcidade.Text) <> "" Then
                If (ValidaEmail(Trim(txtemail.Text))) Then
                    CarregaPropriedades()
                    ret = objBLL.Inserir()
                    If ret = True Then
                        MsgBox("Solicitante " & txtsolicitante.Text & " Inserido com sucesso", MsgBoxStyle.OkOnly)
                        limpa()
                        Atualiza()
                    Else
                        MsgBox("Solicitante " & txtsolicitante.Text & " ja cadastrado", MsgBoxStyle.Information)
                        txtsolicitante.Focus() ' mantem o focu no solicitante
                    End If
                Else
                    MsgBox("Email Inválido", MsgBoxStyle.Information)
                    txtemail.Focus() ' mantem o focu no email
                End If
            Else
                MsgBox("Preencher Todos os campos Obrigatórios", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub

    Private Sub tsbAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbAlterar.Click
        Try
            Dim ret As Boolean
            If Trim(txtsolicitante.Text) <> "" And Trim(txtempresa.Text) <> "" And Trim(txtcidade.Text) <> "" Then
                If (ValidaEmail(Trim(txtemail.Text))) Then
                    CarregaPropriedades()
                    ret = objBLL.Alterar()
                    If ret = True Then
                        MsgBox("Solicitante " & txtsolicitante.Text & " Alterado com sucesso", MsgBoxStyle.OkOnly)
                        tsbVoltar_Click(sender, e)
                        Atualiza()
                    Else
                        MsgBox("Solicitante " & txtsolicitante.Text & " ja cadastrado", MsgBoxStyle.Information)
                        txtsolicitante.Focus() ' mantem o focu no solicitante
                    End If
                Else
                    MsgBox("Email Inválido", MsgBoxStyle.Information)
                    txtemail.Focus() ' mantem o focu no email
                End If
            Else
                MsgBox("Preencher Todos os campos Obrigatórios", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub

    Private Sub txtempresa_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtempresa.Validating
        txtempresa.Text = StrConv(txtempresa.Text, VbStrConv.ProperCase)
    End Sub

    Private Sub txtcidade_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtcidade.Validating
        txtcidade.Text = StrConv(txtcidade.Text, VbStrConv.ProperCase)
    End Sub
End Class